<?php

namespace server\dao;

use common\helpers\ConstantHelper;
use Yii;
use yii\mongodb\Query;

/**
 * This is the model class for collection "devices".
 *
 * @property \MongoDB\BSON\ObjectID|string $_id
 */
class Devices extends \common\core\server\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function collectionName()
    {
        //return 'devices';
        return 'devices';
    }

    /**
     * {@inheritdoc}
     */
    public function attributes()
    {
        return [
            '_id',
            'uuid',
            'software',
            'hardware',
            'timestamp'
            /*'ip',
            'port',
            'username',
            'password'*/
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [

        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            '_id' => 'ID',
            'line_id' => 'Line ID',
            'station_id' => 'station ID',
            'lane_id' => 'lane ID',
            'ip' => 'IP',
            'port' => 'Port',
            'username' => 'Username',
            'password' => 'Password',
        ];
    }

    public function getDevicesList($params = [], $isAll = true)
    {
        $query = new Query();
        $query->from($this->collectionName());
        /*if ($params['type'] == 1) {
            $query->andWhere(['line_id' => $params['id']]);
        } elseif ($params['type'] == 2) {
            $query->andWhere(['station_id' => $params['id']]);
        }*/
        return $isAll ? $query->all() : $this->mongodbPage($query);
    }

    public function getInfoByIP($ip){
        $query = new Query();
        return $data = $query
            ->where(['uuid'=>$ip])
            ->from($this->collectionName())
            ->one();
    }

    public function insertDevices($params)
    {
        $collection = \Yii::$app->mongodb->getCollection($this->collectionName());
        //返回添加后的objectId，不限制于attributes
        $res = $collection->insert($params);
        return $res;

    }
}
